package com.cantierinformatici.mygym;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.CursorAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.LinearLayout;

import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;

import com.cantierinformatici.data.DbFacade;
import com.cantierinformatici.mygym.business.Muscolo;
import com.cantierinformatici.mygym.data.sqlite.Contract;
import com.cantierinformatici.mygym.dialogs.DeleteDialog;
import com.cantierinformatici.mygym.dialogs.EditItemDialog;

public class MuscoliActivity extends SherlockFragmentActivity {
	private ListLayout listView1;
	private MuscoliListAdapter adapter;
	private EditItemDialog mEditDialogFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_list_layout);
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayHomeAsUpEnabled(false);
		listView1 = (ListLayout) findViewById(R.id.edit_list);
		bindList();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.home, menu);
		getSupportMenuInflater().inflate(R.menu.list_attrezzi, menu);
		return true;
	}

	@Override
	protected void onStop() {
		super.onStop();
		try {
			DbFacade.close();
		} catch (Exception error) {
			error.printStackTrace();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		try {
			DbFacade.close();
		} catch (Exception error) {
			error.printStackTrace();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		bindList();
	}

	public void bindList() {
		listView1.removeAllViews();
		int flag = CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER;
		Context context;
		Cursor cursor;
		Muscolo muscoli[] = new Muscolo[] {};
		try {
			context = getApplicationContext();
			cursor = DbFacade.get(context, muscoli);
			if (cursor.getCount() > 0) {
				adapter = new MuscoliListAdapter(context, cursor, flag);
				listView1.setList(adapter);				
			} else {
				listView1.setEmpty();				
			}
			DbFacade.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private class MuscoliListAdapter extends CursorAdapter {

		public MuscoliListAdapter(Context context, Cursor c, int flag) {
			super(context, c, flag);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			//Imposto il titolo
			TextView txt = (TextView) view.findViewById(R.id.list_item_title);
			String nome = cursor.getString(cursor.getColumnIndex(Contract.MuscoloEntry.COLUMN_NAME_NOME));
			txt.setText(nome);
			// Imposto il click sulle opzioni
			long mItemId = cursor.getLong(cursor.getColumnIndex(Contract.MuscoloEntry._ID));
			LinearLayout itemOptions = (LinearLayout) view.findViewById(R.id.item_options);
			OnClickListener mEditListener = getEditListener(mItemId);
			itemOptions.setOnClickListener(mEditListener);

			

		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(context);
			View v = inflater.inflate(R.layout.list_item, parent, true);
			bindView(v, context, cursor);
			return v;
		}
		
		private OnClickListener getEditListener(final long mItemId){
			return new OnClickListener() {
				@Override
				public void onClick(View v) {
					//Creo l'Intent per l'attivita che rinomina
					Intent renameIntent = new Intent(getApplication(),
							com.cantierinformatici.mygym.MuscoloActivity.class);
					renameIntent.setAction(Intent.ACTION_EDIT);
					Bundle renameBundle = new Bundle();
					renameBundle.putLong(Contract.MuscoloEntry._ID, mItemId);
					renameIntent.putExtras(renameBundle);

					//Creo il listener per il dialog di eliminazione
					DialogInterface.OnClickListener mDeleteListener = new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int id) {
							try {
								DbFacade.delete(getApplicationContext(),new Muscolo(mItemId));
								bindList();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					};
					mEditDialogFragment = new EditItemDialog();
					mEditDialogFragment.setDeleteListener(mDeleteListener);
					mEditDialogFragment.setIntent(renameIntent);
					mEditDialogFragment.show(getSupportFragmentManager(), null);

				}
			};
		}
	}

}
